package io.wdb.handler;

import com.wdb.exchange.model.ActivityLoginModel;
import com.wdb.exchange.model.PresentAmountModel;
import com.wdb.exchange.model.PresentVIPModel;
import com.wdb.exchange.model.RespModel;
import com.wdb.exchange.model.User;
import com.wdb.exchange.model.UserReadRecordModel;
import com.wdb.exchange.service.ApiService;

import io.wdb.common.utils.CommonUtil;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ApiServiceHandler {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	ApplicationContext applicationContext;

	public String echo() {
		ApiService service = applicationContext.getBean(ApiService.class);
		try {
			String ret = service.echo("hello");
			logger.info("call api echo " + ret);
			return ret;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}

	}

	public User getWxUserinfoByToken(String usertoken, String userid, String appid) {
		ApiService service = applicationContext.getBean(ApiService.class);
		logger.info("activity request mq apisevice getWxUserinfoByToken:token{},userid{},appid{}", usertoken, userid,
				appid);
		Long userId = null;
		if (!CommonUtil.isEmpty(userid)) {
			userId = Long.valueOf(userid);
		}
		User user = service.getWxUserinfoByToken(usertoken, userId, appid);
		logger.info("activity request end:[{}]", user);
		return user;
	}

	public Map<String, Object> couponBookShelfInfo(String couponRuleId) {
		ApiService service = applicationContext.getBean(ApiService.class);
		logger.info("Activity request mq rpc apiservice couponBookShelfInfo:{}", couponRuleId);
		Map<String, Object> models = service.couponBookShelfInfo(couponRuleId);
		logger.info("Activity request mq rpc apiservice couponBookShelfInfo END:{}", models);
		return models;
	}

	public int returnBookCount(String userid) {
		ApiService service = applicationContext.getBean(ApiService.class);
		logger.info("Activity rpc request apiservice returnBookCount:{}", userid);
		int count = service.getCurrentDateReturnBookCount(userid).getCount();
		logger.info("Activity rpc request apiservice returnBookCount end:{}", count);
		return count;
	}

	/**
	 * 扫码登录
	 * 
	 * @param activityLogin
	 * @return
	 */
	public RespModel activityLogin(String activityPartner, String mobileNo, String verifyCode) {
		RespModel response = null;
		if (CommonUtil.isEmpty(mobileNo) || CommonUtil.isEmpty(verifyCode)) {
			return null;
		}
		ActivityLoginModel activityLogin = new ActivityLoginModel();
		activityLogin.setActivityPartner(activityPartner);
		activityLogin.setMobileNo(mobileNo);
		activityLogin.setVerifyCode(verifyCode);
		logger.info("rpc activityLogin start,{}", activityLogin);
		ApiService service = applicationContext.getBean(ApiService.class);
		response = service.activityLogin(activityLogin);
		logger.info("rpc activityLogin end,{}", response);
		return response;
	}

	/**
	 * 赠送金额
	 * 
	 * @param presentAmount
	 * @return
	 */
	public RespModel presentAmount(Integer amount, String mobileNo) {
		RespModel respModel = null;
		if (CommonUtil.isEmpty(mobileNo) || CommonUtil.isEmpty(amount)) {
			return null;
		}
		PresentAmountModel presentAmount = new PresentAmountModel();
		presentAmount.setAmount(amount);
		presentAmount.setMobileNo(mobileNo);
		logger.info("rpc presentAmount start,{}", presentAmount);
		ApiService service = applicationContext.getBean(ApiService.class);
		respModel = service.presentAmount(presentAmount);
		logger.info("rpc presentAmount end,{}", respModel);

		return respModel;
	}

	/**
	 * 赠送VIP
	 */
	public RespModel presentVIP(String mobileNo, Integer vipType, Integer days) {
		RespModel response = null;
		if (CommonUtil.isEmpty(mobileNo) || CommonUtil.isEmpty(vipType)) {
			return null;
		}
		PresentVIPModel presentVIP = new PresentVIPModel();
		presentVIP.setMobileNo(mobileNo);
		presentVIP.setVipType(vipType);
		logger.info("rpc presentVIP start,{}", presentVIP);
		ApiService service = applicationContext.getBean(ApiService.class);
		response = service.presentVIP(presentVIP);
		return response;
	}

	/**
	 * 会员到期时间
	 */
	public RespModel userVIPEndDate(String mobileNo) {
		RespModel response = null;
		if (CommonUtil.isEmpty(mobileNo)) {
			return null;
		}
		logger.info("rpc userVIPEndDate start,{}", mobileNo);
		ApiService service = applicationContext.getBean(ApiService.class);
		response = service.userVIPEndDate(mobileNo);
		return response;
	}

	/**
	 * 查询用户阅读数
	 */
	public RespModel userReadRecord(Long userid) {
		if (CommonUtil.isEmpty(userid)) {
			return null;
		}
		logger.info("rpc userReadRecord start,{}", userid);

		ApiService service = applicationContext.getBean(ApiService.class);
		RespModel response = service.userReadRecord(userid);
		
		return response;
	}

}
